﻿using System;
using ZiChen.SoXunLeiPan.ViewModel.Result.ValueType;

namespace ZiChen.SoXunLeiPan.ViewModel.Result
{
	/// <summary>
	/// 统一响应结果
	/// </summary>
	public class UnifyResult
	{
		public UnifyResult()
		{
			Code = ResultCode.Success;
			Msg = Code.Description();
		}

		public UnifyResult(ResultCode code, object message = null)
		{
			Code = code;
			Msg = message ?? code.Description();
		}

		public UnifyResult(object message, ResultCode code = ResultCode.Success)
		{
			Msg = message;
			Code = code;
		}

		/// <summary>
		/// 消息
		/// </summary>
		public object Msg { get; set; }

		/// <summary>
		/// 状态值
		/// </summary>
		public ResultCode Code { get; set; }

		/// <summary>
		/// 返回错误消息
		/// </summary>
		/// <param name="message"></param>

		public static implicit operator UnifyResult(string message)
		{
			return new UnifyResult { Msg = message, Code = ResultCode.ValidError };
		}

		/// <summary>
		/// 是否成功
		/// </summary>
		/// <param name="success"></param>
		public static implicit operator UnifyResult(bool success)
		{
			return new UnifyResult
			{
				Code = ResultCode.Success
				,
				Msg = success ? ResultCode.Success.Description() : ResultCode.ValidError.Description()
			};
		}

		/// <summary>
		/// 异常消息
		/// </summary>
		/// <param name="exception"></param>
		public static implicit operator UnifyResult(Exception exception)
		{
			return new UnifyResult
			{
				Code = ResultCode.Error
				,
				Msg = exception.Message
			};
		}

		/// <summary>
		/// 枚举消息
		/// </summary>
		/// <param name="code"></param>
		public static implicit operator UnifyResult(ResultCode code)
		{
			return new UnifyResult
			{
				Code = code
				,
				Msg = code.Description()
			};
		}
	}
}